package com.test.webflux.service.impl;

import com.test.webflux.dao.ProductDao;
import com.test.webflux.entity.Product;
import com.test.webflux.entity.ResponseResult;
import com.test.webflux.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * TODO
 *
 * @Description
 * @Author lingmin
 * @Date 2025/4/14 15:42
 **/
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {



    @Autowired
    private ProductDao productDao;

    @Override
    public Mono<String> getTitle() {
        log.info("title");
        return Mono.just("产品信息");
    }

    @Override
    public Mono<ResponseResult<Product>> list(Product product) {
        log.info("list.product:{}" , product);
        ResponseResult<Product> result = new ResponseResult();
        result.setCode("000000");
        result.setMsg("成功");
        result.setData(product);
        return Mono.just(result);
    }

    @Override
    public Mono<ResponseResult<Product>> save(Mono<Product> mono) {
        //请求需要返回的处理方式
        return mono.flatMap(product -> {
            log.info("save.product:{}" , product);
            ResponseResult<Product> result = new ResponseResult();
            result.setCode("000000");
            result.setMsg("处理成功");
            result.setData(product);
            return Mono.just(result);
        });
    }

    @Override
    public Mono<Void> subscribe(Mono<Product> mono) {
        log.info("subscribe");
        //无返回处理
        // 订阅并处理
        mono.subscribe(
                value -> log.info("接收到数据: " + value), // onNext
                error -> log.info("发生错误: " + error),   // onError
                () -> log.info("处理完成")                // onComplete
        );
        //mono.publish()
        return Mono.empty();
    }

    @Override
    public Mono<ResponseResult<Product>> getProductById(ServerRequest request) {
        String id = request.pathVariable("id");
        Product product = new Product();
        product.setId(id);
        product.setProductNo("productNo");
        product.setName("名称");
        log.info("list.product:{}" , product);
        ResponseResult<Product> result = new ResponseResult();
        result.setCode("000000");
        result.setMsg("成功");
        result.setData(product);
        return Mono.just(result);
    }

    @Override
    public Mono<ServerResponse> save2(ServerRequest request) {
        log.info("save2");
        return request.bodyToMono(Product.class)
                .flatMap(product -> ServerResponse.ok().bodyValue("Created: " + product.getName()));
    }

    @Override
    public Mono<ServerResponse> exeptionHandle(ServerRequest request) {
        log.info("exeptionHandle");
        String id = request.pathVariable("id");
        if("0".equals(id)){
            throw new RuntimeException("id不能为0");
        }
        Product product = new Product();
        product.setId(id);
        product.setProductNo("productNo");
        product.setName("名称");
        log.info("list.product:{}" , product);
        ResponseResult<Product> result = new ResponseResult();
        result.setCode("000000");
        result.setMsg("成功");
        result.setData(product);
        return ServerResponse.ok().bodyValue(result);
    }

    @Override
    public Mono<Product> monoExeptionHandle(ServerRequest request) {
        log.info("monoExeptionHandle");
        String id = request.pathVariable("id");
        return productDao.getProductById(id)
                .doOnNext(product -> log.info("获取信息: {}" , product))
                .doOnError(error -> log.error("查询错误: {}" , error))
                .doOnSuccess(product -> log.info("获取信息成功: {}" , product))
                .switchIfEmpty(Mono.error(new RuntimeException("id记录不存在")));
    }

}
